package cool.mtc.minghe.web.controller.sys.role;

import cool.mtc.minghe.data.model.base.IdForm;
import cool.mtc.minghe.data.model.sys.role.permission.dto.RolePermissionDTO;
import cool.mtc.minghe.data.model.sys.role.permission.form.RolePermissionForm;
import cool.mtc.minghe.service.service.sys.role.RolePermissionService;
import cool.mtc.minghe.web.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 明河
 */
@Tag(name = "角色权限关联关系管理")
@RestController
@RequestMapping("api/sys/role_permission")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class RolePermissionController extends BaseController {
    private final RolePermissionService rolePermissionService;

    @PreAuthorize("@ps.hasPermission('sys:role_permission:insert')")
    @Tag(name = "角色权限关联关系管理")
    @Operation(summary = "新增", description = "权限：sys:role_permission:insert")
    @PostMapping("insert")
    public void insert(@RequestBody @Validated RolePermissionForm form) {
        rolePermissionService.insert(form);
    }

    @PreAuthorize("@ps.hasPermission('sys:role_permission:delete')")
    @Tag(name = "角色权限关联关系管理")
    @Operation(summary = "删除", description = "权限：sys:role_permission:delete")
    @PostMapping("delete")
    public void delete(@RequestBody IdForm form) {
        rolePermissionService.delete(form.getId());
    }

    @PreAuthorize("@ps.hasPermission('sys:role_permission:read')")
    @Tag(name = "角色权限关联关系管理")
    @Operation(summary = "列表", description = "权限：sys:role_permission:read")
    @GetMapping("list/{roleId}")
    public List<RolePermissionDTO> listPermission(@PathVariable("roleId") long roleId) {
        return rolePermissionService.listPermissionByRoleIdo(roleId);
    }
}
